eval 'exec perl -w -S $0 ${1+"$@"}'
 if 0;

use strict;
use warnings;

sub fill
{
    my $href_opl=shift @_;
    my $pallet=shift @_;
    my $carton=shift @_;
    my $imeipack=shift @_;


    if (!exists $href_opl->{"$pallet"})
    {
	my %mylocalhash=();
	$href_opl->{"$pallet"}=\%mylocalhash;
    }

    if (! exists $href_opl->{"$pallet"}->{"$carton"})
    {
	my @mylocallist=();
	$href_opl->{"$pallet"}->{"$carton"}=\@mylocallist;
    }

    push(@{$href_opl->{"$pallet"}->{"$carton"}}, $imeipack);

}

sub myprint
{
    my $href_opl=shift @_;
    my $href_globaldata=shift @_;
    my $pallet;
    my $carton;
    my $data;
    my $counter=0;
    my $palletimeinumber="";
    my $cartonimeinumber="";
    
    # type 0
    $counter++;
    print $href_globaldata->{MANUFACTURER_SUPPLIER_CODE}
    .$href_globaldata->{MOVEMENT_CODE}
    .$href_globaldata->{RECIPIENT_ID}
    ."0"
    .$href_globaldata->{DELIVERY_DATE}
    ."00000000"
    #."<"
    ."\n";

    # type 1
    $counter++;
    print $href_globaldata->{MANUFACTURER_SUPPLIER_CODE}
    .$href_globaldata->{MOVEMENT_CODE}
    .$href_globaldata->{RECIPIENT_ID}
    ."1"
    .$href_globaldata->{MOVEMENT_TYPE}
    .$href_globaldata->{SUPPLIER_REFERENCE}
    .$href_globaldata->{CUSTOMER_REFERENCE}
    .$href_globaldata->{CUSTOMER_SITE_REFERENCE}
    .$href_globaldata->{DELIVERY_DATE}
    #."<"
    ."\n";
    
    foreach $pallet (sort keys %{$href_opl} )
    {
	# type 2
	$counter++;
	$href_globaldata->{OPL_NUMBER}=~s/0*//;
	$href_globaldata->{OPL_NUMBER}=pad($href_globaldata->{OPL_NUMBER}," ",10,"right");
	$href_globaldata->{DELIVERY_DATE}=~s/^(\d{8}).+/$1/;

	print $href_globaldata->{MANUFACTURER_SUPPLIER_CODE}
	.$href_globaldata->{MOVEMENT_CODE}
	.$href_globaldata->{RECIPIENT_ID}
	."2"
	.$href_globaldata->{MOVEMENT_TYPE}
	.$href_globaldata->{PALLET_NUMBER_BEGINNING}
	.$href_globaldata->{CCCC_CODE}
	.$pallet
	."02"
	.$href_globaldata->{FTT_REFERENCE}
	.$href_globaldata->{EAN13_CODE}
	.$href_globaldata->{PRODUCTION_SITE_ID}
	.$href_globaldata->{DELIVERY_DATE}
	.$href_globaldata->{ORDER_NUMBER}
	.$href_globaldata->{OPL_NUMBER}
	#."<"
	."\n";
	#print "Pallet $pallet:\n";

	# type 3
	$counter++;
	#$href_globaldata->{OPL_IMEI_NUMBER}=($href_globaldata->{OPL_IMEI_NUMBER} * 2);
	#$href_globaldata->{OPL_IMEI_NUMBER}=pad($href_globaldata->{OPL_IMEI_NUMBER},0,8,"left");

        my $hash=$href_globaldata->{PALLET_IMEI_NUMBER};
        #foreach my $key (keys %$hash)
        #{
	#print $key." => ".$hash->{$key}."\n";
        #}
	$palletimeinumber=$hash->{$pallet};

	print $href_globaldata->{MANUFACTURER_SUPPLIER_CODE}
	.$href_globaldata->{MOVEMENT_CODE}
	.$href_globaldata->{RECIPIENT_ID}
	."3"
	.$href_globaldata->{MOVEMENT_TYPE}
	.$href_globaldata->{PALLET_NUMBER_BEGINNING}
	.$href_globaldata->{CCCC_CODE}
	.$pallet
	.$href_globaldata->{PALLET_NUMBER_BEGINNING}
	.$href_globaldata->{CCCC_CODE}
	.$pallet
	.$href_globaldata->{PALLET_NUMBER_BEGINNING}
	.$href_globaldata->{CCCC_CODE}
	.$pallet
	."PAL01"
	#.$href_globaldata->{OPL_IMEI_NUMBER}
	#.scalar @{$href_opl->{$pallet}}
	.pad($palletimeinumber,0,8,"left")
	#."<"
	."\n";

	foreach $carton (sort keys %{$href_opl->{$pallet}} )
	{
	    # type 3
	    $counter++;
	    #$href_globaldata->{OPL_IMEI_NUMBER}=($href_globaldata->{OPL_IMEI_NUMBER} * 2);
	    #$href_globaldata->{OPL_IMEI_NUMBER}=leadingZero($href_globaldata->{OPL_IMEI_NUMBER},8);
	    $cartonimeinumber=scalar @{$href_opl->{$pallet}->{$carton}};
	    $cartonimeinumber=($cartonimeinumber * 2);
	    $cartonimeinumber=pad($cartonimeinumber,0,8,"left");

	    print $href_globaldata->{MANUFACTURER_SUPPLIER_CODE}
	    .$href_globaldata->{MOVEMENT_CODE}
	    .$href_globaldata->{RECIPIENT_ID}
	    ."3"
	    .$href_globaldata->{MOVEMENT_TYPE}
	    .$href_globaldata->{PALLET_NUMBER_BEGINNING}
	    .$href_globaldata->{CCCC_CODE}
	    .$pallet
	    .$href_globaldata->{CARTON_NUMBER_BEGINNING}
	    #.$href_globaldata->{CCCC_CODE}
	    ."00"
	    .$carton
	    ."  "
	    .$href_globaldata->{PALLET_NUMBER_BEGINNING}
	    .$href_globaldata->{CCCC_CODE}
	    .$pallet
	    ."CAR02"
	    .$cartonimeinumber
	    #."<"
	    ."\n";

	    foreach $data ( @{$href_opl->{$pallet}->{$carton}} )
	    {
		# type 4
		$counter++;
		my @elements=split(":",$data);
		my $imei=$elements[0];
		my $simcard=$elements[1];
                
                # cropedimei length = 14
		my $cropedimei=$imei;
		$cropedimei=~s/^(\d{14}).+/$1/;
		
		print $href_globaldata->{MANUFACTURER_SUPPLIER_CODE}
		.$href_globaldata->{MOVEMENT_CODE}
		.$href_globaldata->{RECIPIENT_ID}
		."3"
		.$href_globaldata->{MOVEMENT_TYPE}
		.$href_globaldata->{PALLET_NUMBER_BEGINNING}
		.$href_globaldata->{CCCC_CODE}
		.$pallet
		.$href_globaldata->{PACK_NUMBER_BEGINNING}
		.$href_globaldata->{CCCC_CODE}
		.$cropedimei
		.$href_globaldata->{CARTON_NUMBER_BEGINNING}
		#.$href_globaldata->{CCCC_CODE}
		."00"
		.$carton
		."  "
		."PAK03"
		#.$href_globaldata->{OPL_IMEI_NUMBER}
		."00000002"
		#."<"
		."\n";

                # type 4
		# IMEI
		$counter++;
		print $href_globaldata->{MANUFACTURER_SUPPLIER_CODE}
		.$href_globaldata->{MOVEMENT_CODE}
		.$href_globaldata->{RECIPIENT_ID}
		."4"
		.$href_globaldata->{MOVEMENT_TYPE}
		.$href_globaldata->{PALLET_NUMBER_BEGINNING}
		.$href_globaldata->{CCCC_CODE}
		.$pallet
		.$href_globaldata->{PACK_NUMBER_BEGINNING}
		.$href_globaldata->{CCCC_CODE}
		.$cropedimei
		.pad($imei," ",20,"right")
		."01"
		."IMEI"
		."%%%%%%        "
		." " x 108
		."00000001"
		." " x 8
		#."<"
		."\n";

		# type 4
                # NSCE
		$counter++;
		print $href_globaldata->{MANUFACTURER_SUPPLIER_CODE}
		.$href_globaldata->{MOVEMENT_CODE}
		.$href_globaldata->{RECIPIENT_ID}
		."4"
		.$href_globaldata->{MOVEMENT_TYPE}
		.$href_globaldata->{PALLET_NUMBER_BEGINNING}
		.$href_globaldata->{CCCC_CODE}
		.$pallet
		.$href_globaldata->{PACK_NUMBER_BEGINNING}
		.$href_globaldata->{CCCC_CODE}
		.$cropedimei
		.pad($simcard," ",20,"right")
		."02"
		."NSCE"
		."%%%%%%        "
		." " x 108
		."00000001"
		." " x 8
		#."<"
		."\n";
	    }
	}
    }

    # type 9
    $counter++;
    print $href_globaldata->{MANUFACTURER_SUPPLIER_CODE}
    .$href_globaldata->{MOVEMENT_CODE}
    .$href_globaldata->{RECIPIENT_ID}
    ."9"
    .pad($counter,0,9,"left")
    #."<"
    ."\n";
}

sub pad
{
    # <type> string
    # <char> 0," ",...
    # <max char> 1,2,3,...
    # <position> right/left

    my $length;
    my ($type, $char, $maxChars, $position) = @_;
    $length = length $type;

    $char=$char x ($maxChars-$length);

    if($position eq "left")
    {
	$type=~s/$type/$char$type/;
    }
    if($position eq "right")
    {
	$type=~s/$type/$type$char/;
    }
    
    return $type;
}

sub main
{
    my %opl=();
    my %globaldata=();
    my $line="";
    my $linecounter=0;
    my $in;
    #my $file="/data/samba_share/tct-huizhou/opl/07218085.000";
    my $file=$ARGV[0];

    open($in, "< $file") || die "Couldn't open $file.";
    while ( $line= <$in>)
    {
	$linecounter++;
	my @fields=();
	my $imei="";
	my $pallet="";
	my $carton="";
	my $simcard="";

	@fields=split(";",$line);
	$imei=$fields[0];

        # remove zero at the beginning of the string
        # add space at the end of the string to fit the length of the field (14)
	$pallet=$fields[12];
	$pallet=~s/0*//;
	$pallet=pad($pallet," ",14,"right");
	#print "pallet -> |".$pallet."|\n";

	$carton=$fields[15];
	$carton=~s/0*//;
	$carton=pad($carton," ",14,"right");

	$simcard=$fields[3];

	if ($linecounter eq 1)
	{
	    $globaldata{OPL_NUMBER}=$fields[7];
	    $globaldata{MANUFACTURER_SUPPLIER_CODE}="ALC";
	    $globaldata{MOVEMENT_CODE}="ASN";
	    $globaldata{RECIPIENT_ID}="05X";
	    $globaldata{MOVEMENT_TYPE}="C";
	    $globaldata{SUPPLIER_REFERENCE}="ALCATEL ";
	    $globaldata{CUSTOMER_REFERENCE}="HAY";
	    $globaldata{CUSTOMER_SITE_REFERENCE}="HAY";
	    $globaldata{PALLET_NUMBER_BEGINNING}="JP";
	    $globaldata{CARTON_NUMBER_BEGINNING}="JC";
	    $globaldata{PACK_NUMBER_BEGINNING}="JK";
	    $globaldata{CCCC_CODE}="0040";
	    $globaldata{FTT_REFERENCE}="%%%%%%    ";
	    $globaldata{EAN13_CODE}="############# ";
	    $globaldata{PRODUCTION_SITE_ID}="FOURN   ";
	    $globaldata{ORDER_NUMBER}=$fields[10];
	    $globaldata{DELIVERY_DATE}=$fields[19];
	}
	#print "IMEI -> ".$imei."\n";

	if (!exists $globaldata{PALLET_IMEI_NUMBER}{"$pallet"})
	{
	    $globaldata{PALLET_IMEI_NUMBER}{"$pallet"}=$fields[14];
	}

	fill(\%opl,$pallet,$carton,$imei.":".$simcard);
    
    }

    #my $hash=$globaldata{PALLET_IMEI_NUMBER};
    #foreach my $key (keys %$hash)
    #{
	#print $key." => ".$hash->{$key}."\n";
    #}
    

    #print "linecounter -> ".$linecounter."\n";
    #print "NUM_OPL -> ".$globaldata{NUM_OPL}."\n";

    myprint(\%opl,\%globaldata);
}

main();
